package com.sf.web.controller;

import com.sf.domain.Employee;
import com.sf.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @RequestMapping("/login")
    public String login(Model model,HttpSession session, String username, String password){
        try{
            Employee employee = loginService.login(username, password);
            // 如果login 代码没有问题说明 账号和密码输入的是正确的
            // 把用户信息保存到session 当中
            session.setAttribute("USER_IN_SESSION",employee);
            return "redirect:/department/list";
        }catch (Exception e){
            e.printStackTrace();
            //往model 当中存储数据
        model.addAttribute("errorMsg",e.getMessage());
        // 重新跳转到login.jsp 界面
        return "forward:/login.jsp";
        }
        }
        }
